package com.qf.postprocessor;

import com.qf.pojo.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author lwc
 * @date 2021/10/26 13:22
 * @Description
 */
public class MyBeanPost implements BeanPostProcessor {

    //在bean创建之前可以用下面的方法 来修改
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在bean 初始化之前执行");

        return bean;
    }

    //bean初始化完成之后 利用下面的方法可以进行更改
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("在bean初始化之后执行");

        if (bean instanceof User) {
            User user = (User) bean;
            user.setName("貂蝉");
            return user;
        }
        return bean;
    }
}